<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: 'ID' })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <!-- 表单 -->
        <PopupForm ref="formRef" />
    </div>
</template>

<script setup lang="ts">
import { provide, useTemplateRef } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { fullUrl } from '/@/utils/common'

defineOptions({
    name: 'vip/vip',
})
const formRef = useTemplateRef('formRef')

const { t } = useI18n()
const baTable = new baTableClass(
    new baTableApi('vip/vip/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            { label: 'VIP等级', prop: 'level', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: '名称', prop: 'name', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            {
                label: '标签',
                prop: 'label',
                align: 'center',
                operator: false,
                render: 'tags',
                formatter(row, column, cellValue) {
                    if (cellValue) {
                        return cellValue.split(',')
                    }
                    return []
                },
            },
            { label: '介绍', prop: 'intro', align: 'center' },
            {
                label: '图片',
                prop: 'image',
                align: 'center',
                render: 'image',
                operator: false,
                formatter(row, column, cellValue) {
                    if (cellValue) {
                        return fullUrl(cellValue)
                    }
                    return ''
                },
            },
            {
                label: '销量',
                prop: 'sales',
                align: 'center',
            },
            { label: t('Create time'), prop: 'createtime', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
            {
                label: t('State'),
                prop: 'status_text',
                align: 'center',
                render: 'tag',
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: defaultOptButtons(['edit', 'delete']),
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            status: 'normal',
            sales: 0,
        },
    },
    {},
    {
        getEditData({ res }) {
            if (res && res.data) {
                baTable.form.items = res.data
                formRef.value!.priceList = JSON.parse(res.data.pricedata)
                formRef.value!.rightList = JSON.parse(res.data.rightdata)
            }
        },
    }
)

// 利用提交前钩子重写提交操作
baTable.before.onSubmit = ({ formEl, operate, items }) => {
    items.pricedata = formRef.value?.priceList
    items.rightdata = formRef.value?.rightList
    baTable.form.items!.pricedata = JSON.stringify(items.pricedata)
    baTable.form.items!.rightdata = JSON.stringify(items.rightdata)

    let submitCallback = () => {
        baTable.form.submitLoading = true
        baTable.api
            .postData(operate, items)
            .then((res) => {
                baTable.onTableHeaderAction('refresh', {})
                baTable.form.submitLoading = false
                baTable.form.operateIds?.shift()
                if (baTable.form.operateIds!.length > 0) {
                    baTable.toggleForm('Edit', baTable.form.operateIds)
                } else {
                    baTable.toggleForm()
                }
                baTable.runAfter('onSubmit', { res })
            })
            .catch(() => {
                baTable.form.submitLoading = false
            })
    }

    if (formEl) {
        baTable.form.ref = formEl
        formEl.validate((valid) => {
            if (valid) {
                submitCallback()
            }
        })
    } else {
        submitCallback()
    }
    return false
}

baTable.mount()
baTable.getData()

provide('baTable', baTable)
</script>

<style scoped lang="scss"></style>
